<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-inline seller-inline-3" style="width: 150px;">
                <select name="card_type" lay-verify="" >
                    <option value="">Card Type</option>
                    <option value="556305">556305</option>
                    <option value="556766">556766</option>
                    <option value="558068">558068</option>
                    <option value="531847">531847</option>
                    <option value="556735">556735</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <input type="text" name="user_name" lay-verify="title" placeholder="User Name" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-inline">
            <input type="text" name="card_num" id="card_num" lay-verify="title" placeholder="Card Num" autocomplete="off"
                   class="layui-input" value="{$card_num}">
        </div>
        <div class="layui-inline">
            <input type="text" name="desc" lay-verify="title" placeholder="Desc" autocomplete="off"
                   class="layui-input">
        </div>
        <div class="layui-inline">
            <input type="text" name="datetime" id="datetime" placeholder="Start Time _ End Time" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="*"><i class="iconfont icon-chaxun"></i>Search</button>
            </div>
        </div>
    </div>
</form>
<div class="table-body">
    <table id="balanceTable" lay-filter="balanceTable"></table>
</div>
<div class="table-body">
    <table id="balanceTable2" lay-filter="balanceTable2"></table>
</div>
<script type="text/html" id="toolbar">
    <span>CardNum : <span style="font-size: 18px;color: green">{$card_num} </span>;
    <span>BankType : <span style="font-size: 18px;color: green">{$bank_type} </span>;<br/>
    <span style="font-size: 20px;color: red">Comdata : </span>
    <span>cycleAmountLimitAvailable: <span style="font-size: 18px;color: red">$ {$api_amount} </span>;
    <span>cycleAmountLimitAvailable: <span style="font-size: 18px;color: red">$ {$api_amount} </span>;
    <span>cycleAmountLimit: <span style="font-size: 18px;color: red">$ {$api_amount_total} </span>;
    <span>cycleAmountLimitHold: <span style="font-size: 18px;color: red">$ {$amount_hold} </span>;
    <span>cycleAmountLimitUsed: <span style="font-size: 18px;color: red">$ {$amount_used} </span>;<br/>
    <span style="font-size: 20px;color: #d39e00">Connexpay : </span>
    <span>availableBalance: <span style="font-size: 18px;color: #d39e00">$ {$availableBalance} </span>;
    <span>amountLimit: <span style="font-size: 18px;color: #d39e00">$ {$amountLimit} </span>;
</script>
<script>
    layui.use(['form', 'laydate', 'table',], function () {
        var laydate = layui.laydate,
            form = layui.form,
            table = layui.table; //表格
        //时间插件
        laydate.render({
            elem: '#datetime',
            range: '_',
            format: 'yyyy-MM-dd'
        });
        table.render({
            elem: '#balanceTable',
            height: 'auto',
            cellMinWidth: '80',
            totalRow: true,
            where:{card_num : $('#card_num').val()},
            method: 'post',
            url: "{:url('Cards/cardLog')}",
            id: 'balanceTable',
            cols: [[
                { type: 'numbers'},
                { field: 'username',  align: 'center', title: 'UserName',width: 200},
                { field: 'card_num', title: 'CardNum/Memo',width: 200,
                    templet:function (d) {
                        var num = d.card_num;
                        if(num == null){
                            return '--';
                        }else{
                            return " **** " + num.substr(-4) + " / <span style='font-size: 15px;font-weight: bold'>" + d.memo + "</span>";
                        }

                    },align:"right"},
                {field: 'amount', title: 'Amount($)', align:"right",
                    style:'color:#FF5722;font-weight:bold' , width:100 , totalRow: true},
                {field: 'balance', title: 'Balance($)', align:"right",
                    style:'color:#FF5722;font-weight:bold' , width:100},
                { field: '', align: 'left', title: 'Type' ,width:180,
                    templet: function (d) {
                        const type_id = d.type;
                        var  html ='';
                        switch (type_id) {
                            case '1':
                                html = 'Create Card';
                                break;
                            case '2':
                                html = 'Add Amount';
                                break;
                            case '3':
                                html = 'Deduct Amount';
                                break;
                            case '4':
                                html = 'Cancel Card';
                                break;
                        }
                        return html;
                    }
                },
                { field: 'desc',  align: 'left', title: 'Description'},
                { align: 'center', title: 'Create Time',width:160,
                    templet: function (d) {
                        return layui.util.toDateString(d.ctime * 1000, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { field: 'update_time', align: 'center', title: 'UpdateTime',width:160,
                    templet: function (d) {
                        const utime = d.utime;
                        if(utime == null){
                            return '--';
                        }else{
                            return layui.util.toDateString(utime * 1000, "yyyy-MM-dd HH:mm:ss");
                        }
                    }
                },
                { field: 'id',  align: 'left', title: 'table_id'},
            ]],
            page: true,
            limit: 20,
            limits: [20, 50, 100],
        });
        table.render({
            elem: '#balanceTable2',
            height: 'auto',
            cellMinWidth: '80',
            totalRow: true,
            where:{card_num : $('#card_num').val()},
            method: 'post',
            url: "{:url('Cards/cardTran')}",
            id: 'balanceTable2',
            //where:{tran_token : $('#tran_token').val()},
            toolbar: '#toolbar', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports'],
            cols: [[
                { type: 'numbers'},
                { field: 'username',  align: 'left', title: '会员',width: 180},
                { field: 'app_code',  align: 'left', title: 'AppCode',width: 90},
                { field: 'card_num', title: '卡号', align:"left",width: 160},
                { field: 'type',  align: 'left', title: '交易状态',width: 110,
                    templet:function (d) {
                        const type = d.tran_status;
                        const flag = d.reversal_flag;
                        var color = '';
                        if(type == 'Declined'){
                            color = 'red';
                        }else if(type == 'Posted'){
                            color = 'green';
                        }else if(type == 'Authorized'){
                            if(flag == 'X' || flag == 'Y'){
                                color = '#00aeef';
                            }else{
                                color = 'gray';
                            }
                        }else if(type == 'Credit'){
                            color = '#00aeef';
                        }
                        return "<span style='color:" + color + ";font-weight:bold'>" + type + "</span>"
                    },
                    totalRowText:'Total:'},
                { field: 'reversal_flag',  align: 'left', title: 'Flag',width: 60},
                { field: 'shopping',  align: 'left', title: '商家'},
                { field: 'desc',  align: 'left', title: '备注'},
                { field: 'amount_auth',  align: 'right', title: '预扣款($)',width: 120,
                    style:'color:#FF5722;font-weight:bold',totalRow: true},
                { field: 'ctime', align: 'center', title: '交易时间',width: 170},
                { field: 'amount_post',  align: 'right', title: '实际扣款($)',width: 120,
                    style:'color:#FF5722;font-weight:bold',totalRow: true},
                { field: 'utime', align: 'center', title: '结算时间',width: 170}
            ]],
            page: true,
            limit: 20,
            limits: [20, 50, 100 ,500 ,1000],
        });
        form.on('submit(*)', function (data) {
            layui.table.reload('balanceTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            layui.table.reload('balanceTable2', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>